export const dateFormat = (timestamp : number | string | Date, format = "YYYY-MM-DD HH:mm:ss") : string => {
	const date = new Date(timestamp)
	function fixedTwo(value : number) : string {
		return value < 10 ? "0" + value : String(value)
	}
	let showTime = format
	if (showTime.includes("SSS")) {
		const S = date.getMilliseconds()
		showTime = showTime.replace("SSS", "0".repeat(3 - String(S).length) + S)
	}
	if (showTime.includes("YY")) {
		const Y = date.getFullYear()
		showTime = showTime.includes("YYYY")
			? showTime.replace("YYYY", String(Y))
			: showTime.replace("YY", String(Y).slice(2, 4))
	}
	if (showTime.includes("M")) {
		const M = date.getMonth() + 1
		showTime = showTime.includes("MM") ? showTime.replace("MM", fixedTwo(M)) : showTime.replace("M", String(M))
	}
	if (showTime.includes("D")) {
		const D = date.getDate()
		showTime = showTime.includes("DD") ? showTime.replace("DD", fixedTwo(D)) : showTime.replace("D", String(D))
	}
	if (showTime.includes("H")) {
		const H = date.getHours()
		showTime = showTime.includes("HH") ? showTime.replace("HH", fixedTwo(H)) : showTime.replace("H", String(H))
	}
	if (showTime.includes("m")) {
		const m = date.getMinutes()
		showTime = showTime.includes("mm") ? showTime.replace("mm", fixedTwo(m)) : showTime.replace("m", String(m))
	}
	if (showTime.includes("s")) {
		const s = date.getSeconds()
		showTime = showTime.includes("ss") ? showTime.replace("ss", fixedTwo(s)) : showTime.replace("s", String(s))
	}
	if (showTime.includes("ww")) {
		const date1 = new Date(date.getFullYear(), 0, 1)
		const date2 = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 1)
		const dayMS = 24 * 60 * 60 * 1000
		const firstDay = (7 - date1.getDay()) * dayMS
		const weekMS = 7 * dayMS
		const timeDate1 = date1.getTime()
		const timeDate2 = date2.getTime()
		const w = Math.ceil((timeDate2 - timeDate1 - firstDay) / weekMS) + 1
		showTime = showTime.replace("ww", w + "")
	}
	return showTime
}